﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BLL;
using DAL;
using System.Drawing;
using System.IO;
using System.Collections.Generic;
using Telerik.Web.UI;

public partial class Baiviet_Thongtinkhac : System.Web.UI.UserControl
{
    BAIVIETBLL bvBll = new BAIVIETBLL();
    THONGTINKHACBLL ttkBll = new THONGTINKHACBLL();

    protected void Page_Load(object sender, EventArgs e)
    {
        //show files and images to page
        if (!Page.IsPostBack)
        {
            //set visibale for Khach
            VAITRO vt = (VAITRO)Session["vaitroObject"];
            if (vt.tenvaitro.Equals("Khách"))
            {
                btn_edit.Visible = false;
                Button1.Visible = false;
                fieldset_upload.Visible = false;
            }
            else
            {
                THANHVIEN checkTv = (THANHVIEN)Session["thanhvienObject"];
                THANHVIEN ttvietbai = bvBll.getThanhvienWithIDBaiviet(Int32.Parse(Page.Request["mabv"]));
                if (ttvietbai.mathanhvien != checkTv.mathanhvien)
                {
                    btn_edit.Visible = false;
                    Button1.Visible = false;
                    fieldset_upload.Visible = false;
                }
            }

            if (Request.Params["mabv"] != null)
            {
                THONGTINKHAC ttk = ttkBll.getThongtinkhacWithIDBaiviet(Int32.Parse(Page.Request["mabv"]));
                lb_content_ttk.Text = ttk.noidung;
                lb_ngaytao_ttk.Text = "(" + ttk.ngaytao.ToLongDateString() + " - " + ttk.ngaytao.ToLongTimeString() + " )";

                show_list_image();
                if (ListBox1.Items.Count > 0)
                {
                    HyperLink1.ImageUrl = "~/Baiviet/Source/" + Request.Params["mabv"] + "/Thongtinkhac/Images/" + ListBox1.Items[0].Text;
                    HyperLink1.NavigateUrl = "~/Baiviet/ShowImage.aspx?tenbv=" + Request.Params["mabv"] + "&tenhinh=" + ListBox1.Items[0].Text;
                }
                else
                {
                    HyperLink1.ImageUrl = "~/Baiviet/Source/no_image.png";
                }
                show_list_file();
            }
        }
    }

    public void show_list_image()
    {
        ListBox1.Items.Clear();

        DirectoryInfo diFiles = new DirectoryInfo(Server.MapPath("~/Baiviet/Source/" + Request.Params["mabv"] + "/Thongtinkhac/Images/"));
        ListBox1.DataSource = diFiles.GetFiles("*");
        ListBox1.DataBind();
    }

    public void show_list_file()
    {
        string path_test = "Baiviet/Source/" + Request.Params["mabv"] + "/Thongtinkhac/Files/";
        string path = Server.MapPath("~/Baiviet/Source/" + Request.Params["mabv"] + "/Thongtinkhac/Files/");
        string str_inner = "";

        DirectoryInfo diFiles = new DirectoryInfo(path);
        foreach (FileInfo file_info in diFiles.GetFiles("*"))
        {
            str_inner += "<a href=\"" + path_test + file_info.Name + "\">" + file_info.Name + " (" + file_info.Length + "KB)" + " - " + "</a><br>";
        }
        div_hs.InnerHtml = str_inner;
    }

    protected void btn_edit_Click(object sender, EventArgs e)
    {
        rad_ttk.Visible = true;
        rad_ttk.Content = lb_content_ttk.Text;
        btn_ok.Visible = true;
        btn_edit.Visible = false;
    }
    protected void btn_ok_Click(object sender, EventArgs e)
    {
        THONGTINKHAC ttk = ttkBll.getThongtinkhacWithIDBaiviet(Int32.Parse(Page.Request["mabv"]));

        rad_ttk.Visible = false;
        lb_content_ttk.Text = rad_ttk.Content;
        btn_edit.Visible = true;
        btn_ok.Visible = false;

        //update Thongtinkhac
        ttkBll.updateThongtinkhacContentWithId(ttk.matt, rad_ttk.Text);
    }
    protected void Button2_Click(object sender, EventArgs e)
    {

        if (Request.Params["mabv"] != null)
        {
            try
            {
                HyperLink1.ImageUrl = "~/Baiviet/Source/" + Request.Params["mabv"] + "/Thongtinkhac/Images/" + ListBox1.SelectedItem.Text;
                HyperLink1.NavigateUrl = "~/Baiviet/ShowImage.aspx?mabv=" + Request.Params["mabv"] + "&tenhinh=" + ListBox1.SelectedItem.Text + "&type=Thongtinkhac";
                Label1.Text = ListBox1.SelectedItem.Text;
            }
            catch { }
        }
        show_list_image();
    }
    protected void Unnamed1_Click(object sender, EventArgs e)
    {
        int count = ListBox1.Items.Count;
        for (int i = 0; i < count; i++)
        {
            if (ListBox1.Items[i].Selected)
            {
                //delete items
                string file_name_delete = Server.MapPath("~/Baiviet/Source/" + Request.Params["mabv"] + "/Thongtinkhac/Images/" + ListBox1.Items[i].Text);
                File.Delete(file_name_delete);
            }
        }

        for (int i = ListBox1.Items.Count - 1; i >= 0; i--)
        {
            if (ListBox1.Items[i].Selected)
            {
                //delete items
                ListBox1.Items.Remove(ListBox1.Items[i]);
            }
        }
        //reload label and image
        if (ListBox1.Items.Count <= 0) return;
        ListBox1.SelectedIndex = 0;
        Label1.Text = ListBox1.SelectedItem.Text;
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        string path = Server.MapPath("~/Baiviet/Source/" + Request.Params["mabv"] + "/Thongtinkhac/");
        foreach (UploadedFile file in RadAsyncUpload1.UploadedFiles)
        {
            string extension = file.GetExtension();
            if (extension.Equals(".pdf") || extension.Equals(".rar") || extension.Equals(".zip") || extension.Equals(".doc") || extension.Equals(".docx"))
            {
                string tr_path = path + ("Files/" + file.FileName);
                file.SaveAs(tr_path);

            }
            else
            {
                string str_path = path + ("Images/" + file.FileName);
                file.SaveAs(str_path);
            }

        }
        Response.Redirect("Baiviet.aspx?mabv=" + Page.Request["mabv"] + "&tenbv=" + Page.Request["tenbv"]);
    }
}
